﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Resto.CostControl.Core.Models;
using System.Web.Mvc;
using QuickApp.Core;
using QuickApp.MVCControl;
using Resto.CostControl.Core.Models.SysSettings;

namespace Resto.CostControl.Core.Services
{
    public class DicEnumService : IEnumService
    {
        /// <summary>
        /// 获取指定枚举Key的下拉枚举数据源
        /// </summary>
        /// <param name="EnumKey">枚举Key</param>
        /// <returns>枚举数据源</returns>
        public IList<SelectListItem> GetEnumDataSource(string enumKey)
        {
            List<SelectListItem> list = new List<SelectListItem>();
            if (enumKey == "dictype")
            {
                var repo = DependencyResolver.Current.GetService<IRepository<EnumType>>();
                list = repo.FindAll().OrderBy(t => t.SeqNo)
                    .Select(type => new SelectListItem { Text = type.TypeName, Value = type.TypeCode })
                    .ToList();
            }
            else if (enumKey == "PageSize")
            {
                list.Add(new SelectListItem { Text = "1", Value = "1" });
                list.Add(new SelectListItem { Text = "20", Value = "20" });
                list.Add(new SelectListItem { Text = "30", Value = "30" });
                list.Add(new SelectListItem { Text = "50", Value = "50" });
                list.Add(new SelectListItem { Text = "100", Value = "100" });
                list.Add(new SelectListItem { Text = "所有记录", Value = "10000" });
            }
            else if (enumKey == "Material")
            {
                var repo = DependencyResolver.Current.GetService<IRepository<Material>>();
                list = repo.FindAll().OrderBy(t => t.Name)
                    .Select(m => new SelectListItem { Text = m.Name, Value = m.Id })
                    .ToList();
            }
            else if (enumKey == "Supplier")
            {
                var repo = DependencyResolver.Current.GetService<IRepository<Supplier>>();
                list = repo.FindAll().OrderBy(t => t.Name)
                    .Select(s => new SelectListItem { Text = s.Name, Value = s.Id })
                    .ToList();
            }
            else if (enumKey == "ProjectSite")
            {
                var repo = DependencyResolver.Current.GetService<IRepository<ProjectSite>>();
                list = repo.FindAll().OrderBy(t => t.Name)
                    .Select(s => new SelectListItem { Text = s.Name, Value = s.Id })
                    .ToList();
            }
            else
            {
                var repo = DependencyResolver.Current.GetService<IRepository<EnumItem>>();
                list = repo.FindAll(item => item.TypeCode == enumKey).OrderBy(t => t.SeqNo)
                    .Select(item => new SelectListItem { Text = item.EnumName, Value = item.EnumCode })
                    .ToList();
            }

            return list;
        }

        /// <summary>
        /// 获取指定枚举项值的枚举项文本
        /// </summary>
        /// <param name="enumKey">枚举Key</param>
        /// <param name="value"></param>
        /// <returns></returns>
        public string GetEnumText(string enumKey, string value)
        {
            var selectedItem = GetEnumDataSource(enumKey).Where(item => item.Value == value).FirstOrDefault();
            if (selectedItem != null)
            {
                return selectedItem.Text;
            }
            else
            {
                return string.Empty;
            }
        }

        /// <summary>
        /// 获取指定枚举值的文本内容
        /// </summary>
        /// <param name="enumKey">枚举类型Key</param>
        /// <param name="value">枚举项的值</param>
        /// <returns></returns>
        public static string GetDicEnumText(string enumKey, string value)
        {
            var text = DependencyResolver.Current.GetService<IEnumService>().GetEnumText(enumKey, value);
            return text;
        }
    }
}
